package backend.product.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import backend.category.manager.CategoryManager;
import backend.paging.Paging;
import backend.product.manager.ProductAttributeSetManager;
import backend.product.manager.ProductManager;
import backend.product.manager.ProductTypeManager;
import backend.product.manager.UserManager;

import com.opensymphony.xwork2.ActionSupport;

import core.attribute.model.Attribute;
import core.attribute.model.AttributeSet;
import core.category.model.Category;
import core.price.model.Price;
import core.product.model.Product;
import core.product.model.ProductAv;
import core.product.model.ProductOwner;
import core.product.model.ProductType;
import core.profile.model.BasicProfile;

public class ProductController extends ActionSupport 
{
	private static final long serialVersionUID = 1L;
	private List<Product> products ;
	private int pageSize ;
	private int p ;
	private int totalPage ;
	private ProductManager pm ;
	private CategoryManager cm ;
	private UserManager um ;
	private Long uuid ;
	private Long productTypeUuid ;
	private Product product ;
	private Set<ProductType> productType ;
	private List<ProductType> allProductType ;
	private Set<Price> prices ;
	private ProductAv productAv ;
	private List <Category> productCategories ;
	private List<AttributeSet> attributeSet ;
	private Long attributeSetId ;
	private List<Attribute> attributes ;
	private ProductOwner productOwner ;
	private Long productOwnerId ;
	private BasicProfile owner ;
	private Long ownerId ;
	private String managedBy;
	
	public ProductController()
	{
		pm = new ProductManager();
		new ProductTypeManager();
		new ProductAttributeSetManager();
		cm = new CategoryManager();
		um = new UserManager();
	}
	// Action methods
	public String execute() 
	{
		if(pageSize == 0)
		{
			this.setPageSize(10);
		}
		Paging<Product> paging = new Paging<Product>();
		this.setProducts(pm.getProductList());
		this.setProducts(paging.getPageList(this.pageSize, this.products, this.p));
		this.setTotalPage(paging.getPages(this.products.size(), this.pageSize));	
		return SUCCESS ;
	}

	public String editProduct()
	{
		List<Category> cate = new ArrayList<Category>();
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		Iterator <Long> cateId = pm.getProductCategoriesId(this.getUuid()).iterator();
		while(cateId.hasNext())
		{
			cate.add(cm.getCategoryByUuid(cateId.next()));
		}
		this.setProductCategories(cate);
		return SUCCESS ;
	}
	public String productOwner()
	{
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		ProductOwner po =  pm.getProductOwner(this.getProduct()) ;
		if(po == null)
		{
			po = new ProductOwner();
			po.setProduct(this.getProduct());
			pm.saveProductOwner(po);
		}
		
		this.setProductOwner(po);	
		return SUCCESS ;
	}
	public String updateProductOwner()
	{
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		ProductOwner po = pm.getProductOwnerByUuid(this.getProductOwnerId());	
		BasicProfile bp = um.getUserByUuid(this.getOwnerId());
		if(bp != null)
		{
			po.setOwner(bp);
			po.setManagedBy(this.getManagedBy());
			pm.saveProductOwner(po);
		}
		this.setProductOwner(po);
		return SUCCESS ;
	}
	public String producttype()
	{
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		this.setAllProductType(pm.getgetAllProductType());	
		this.setProductType(this.getProduct().getProductType());
		return SUCCESS ;
	}
	public String removeProductType()
	{
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		ProductType pt = ProductTypeManager.getProductTypeByUuid(this.getProductTypeUuid());
		pm.removeProductType(this.product, pt);
		this.setAllProductType(pm.getgetAllProductType());	
		this.setProductType(this.getProduct().getProductType());
		return SUCCESS ;
	}
	public String assignProductType()
	{
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		ProductType pt = ProductTypeManager.getProductTypeByUuid(this.getProductTypeUuid());
		pm.assignProductType(this.product, pt);
		this.setAllProductType(pm.getgetAllProductType());	
		this.setProductType(this.getProduct().getProductType());
		return SUCCESS ;
	}
	public String price()
	{
		this.setProduct(pm.getProductByUuid(this.getUuid()));
		this.setPrices(this.product.getPrice());
		return SUCCESS ;
	}
	public String addPrices()
	{
		return SUCCESS ;
	}

	// setter getter methods
	public Set<Price> getPrices() {
		return prices;
	}
	public void setPrices(Set<Price> prices) {
		this.prices = prices;
	}
	
	public Long getProductTypeUuid() {
		return productTypeUuid;
	}
	public void setProductTypeUuid(Long productTypeUuid) {
		this.productTypeUuid = productTypeUuid;
	}
	public List<ProductType> getAllProductType() {
		return allProductType;
	}
	public void setAllProductType(List<ProductType> allProductType) {
		this.allProductType = allProductType;
	}
	public Set<ProductType> getProductType() {
		return productType;
	}
	public void setProductType(Set<ProductType> productType) {
		this.productType = productType;
	}
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	public Long getUuid() {
		return uuid;
	}
	public void setUuid(Long uuid) {
		this.uuid = uuid;
	}
	
	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getP() {
		return p;
	}

	public void setP(int p) {
		this.p = p;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	public ProductAv getProductAv() {
		return productAv;
	}
	public void setProductAv(ProductAv productAv) {
		this.productAv = productAv;
	}
	public List<AttributeSet> getAttributeSet() {
		return attributeSet;
	}
	public void setAttributeSet(List<AttributeSet> attributeSet) {
		this.attributeSet = attributeSet;
	}
	public Long getAttributeSetId() {
		return attributeSetId;
	}
	public void setAttributeSetId(Long attributeSetId) {
		this.attributeSetId = attributeSetId;
	}
	public List<Attribute> getAttributes() {
		return attributes;
	}
	public void setAttributes(List<Attribute> attributes) {
		this.attributes = attributes;
	}
	public List<Category> getProductCategories() {
		return productCategories;
	}
	public void setProductCategories(List<Category> productCategories) {
		this.productCategories = productCategories;
	}
	public ProductOwner getProductOwner() {
		return productOwner;
	}
	public void setProductOwner(ProductOwner productOwner) {
		this.productOwner = productOwner;
	}
	public BasicProfile getOwner() {
		return owner;
	}
	public void setOwner(BasicProfile owner) {
		this.owner = owner;
	}
	public Long getOwnerId() {
		return ownerId;
	}
	public void setOwnerId(Long ownerId) {
		this.ownerId = ownerId;
	}
	public Long getProductOwnerId() {
		return productOwnerId;
	}
	public void setProductOwnerId(Long productOwnerId) {
		this.productOwnerId = productOwnerId;
	}
	public String getManagedBy() {
		return managedBy;
	}
	public void setManagedBy(String managedBy) {
		this.managedBy = managedBy;
	}	
}
